import Vue from 'vue';
import { placeholderImg } from '@/lib/utils';
import CustomNav from '@/components/nav/customNav.vue';
import ListFooter from '@/components/list/footer.vue';
import ListEmpty from '@/components/list/listEmpty.vue';
import { currency, currencyPreserve } from '@/lib/utils';

export default function extendVue() {
    Vue.directive('clipboard', {
        bind(el, binding) {
            const cb = (e) => {
                wx.setClipboardData({
                    data: e.target.__text,
                    success: () => {
                        wx.showToast({
                            title: '复制成功',
                            icon: 'none',
                        });
                    },
                    fail: () => {
                        wx.showToast({
                            title: '复制失败',
                            icon: 'none',
                        });
                    },
                });
            };
            el.addEventListener('click', cb);
            el.__text = binding.value;
            el.__cb = cb;
        },
        update(el, binding) {
            // @ts-ignore
            el.__text = binding.value;
        },
        unbind(el) {
            // @ts-ignore
            const cb = el.__cb;
            if (cb) {
                el.removeEventListener('click', cb);
            }
            // @ts-ignore
            el.__cb = null;
            // @ts-ignore
            el.__text = null;
        },
    });

    Vue.component('CustomNav', CustomNav);
    Vue.component('ListFooter', ListFooter);
    Vue.component('ListEmpty', ListEmpty);
    Vue.filter('currency', currency);
    Vue.filter('currencyPreserve', currencyPreserve);

    Vue.prototype.placeholderImg = placeholderImg;
}
